package com.example.testrecorder;

import android.os.Build;
import android.os.Bundle;

import androidx.annotation.RequiresApi;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.testrecorder.domain.QueAndAns;
import com.example.testrecorder.domain.User;

import org.litepal.LitePal;

import java.util.List;

/**
 * @author Yingyong Lao
 * 创建时间 2021/5/23 21:42
 * @version 1.0
 */
public class SecurityQuestionFragment extends Fragment {
    private TextView question1Tv,question2Tv,question3Tv,answer1Tv,answer2Tv,answer3Tv;
    private User currUser;
    private Button editBtn;
    private Toolbar toolbar;


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_security_question, container, false);
        question1Tv=v.findViewById(R.id.question1Tv);
        question2Tv=v.findViewById(R.id.question2Tv);
        question3Tv=v.findViewById(R.id.question3Tv);
        answer1Tv=v.findViewById(R.id.answer1Tv);
        answer2Tv=v.findViewById(R.id.answer2Tv);
        answer3Tv=v.findViewById(R.id.answer3Tv);
        currUser=MyApplication.currUser;
        List<QueAndAns> queAndAns = LitePal.where("user_id=?", "" + currUser.getId()).find(QueAndAns.class);
        if(queAndAns.size()==3){
            QueAndAns queAndAns1 = queAndAns.get(0);
            QueAndAns queAndAns2 = queAndAns.get(1);
            QueAndAns queAndAns3 = queAndAns.get(2);
            question1Tv.setText(queAndAns1.getQuestion());
            answer1Tv.setText(queAndAns1.getAnswer());
            question2Tv.setText(queAndAns2.getQuestion());
            answer2Tv.setText(queAndAns2.getAnswer());
            question3Tv.setText(queAndAns3.getQuestion());
            answer3Tv.setText(queAndAns3.getAnswer());
        }else if(queAndAns.size()>3){
            Toast.makeText(getContext(),"密保的问题个数超过3个，请联系管理员！",Toast.LENGTH_SHORT).show();
        } else {
            question1Tv.setText("-");
            answer1Tv.setText("-");
            question2Tv.setText("-");
            answer2Tv.setText("-");
            question3Tv.setText("-");
            answer3Tv.setText("-");
        }
        editBtn=v.findViewById(R.id.editBtn);
        editBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PersonalDataActivity activity = (PersonalDataActivity) getActivity();
                FragmentManager supportFragmentManager = activity.getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = supportFragmentManager.beginTransaction();
                EditSQFragment editSQFragment = new EditSQFragment();
                fragmentTransaction.replace(R.id.container,editSQFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
            }
        });
        toolbar=v.findViewById(R.id.toolbar);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goToViewFra();
            }
        });
        return v;
    }
    private void goToViewFra() {
        PersonalDataActivity activity = (PersonalDataActivity) getActivity();
        FragmentTransaction fragmentTransaction = activity.fragmentManager.beginTransaction();
        ViewPerDataFragment viewPerDataFragment = new ViewPerDataFragment();
        fragmentTransaction.replace(R.id.container,viewPerDataFragment);
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();
    }
}